package com.qianguo.shop.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.utils.IconfontUtil;
import com.qianguo.shop.R;
import com.qianguo.shop.assistant.ShopAssistantManagerActivity;
import com.qianguo.shop.base.BaseTitleActivity;
import com.qianguo.shop.constant.ShopKeys;
import com.eva.androidlibrary.api.entity.ShopInfo;
import com.eva.androidlibrary.icon.FruitIcon;
import com.qianguo.shop.entity.event.MainTabEvent;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;


/**
 * 店铺详情
 *
 * @author GUXIA318
 * @date 2015-10-19 14:31
 */
@EActivity(R.layout.activity_shop_detail)
public class ShopDetailActivity extends BaseTitleActivity {

    @ViewById(R.id.shop_detail_product_icon_text_view)
    TextView mTvIconProducts;
    @ViewById(R.id.shop_detail_assistant_icon_text_view)
    TextView mTvIconAssistants;
    @ViewById(R.id.shop_detail_mailman_icon_text_view)
    TextView mTvIconMailMen;
    @ViewById(R.id.shop_detail_message_icon_text_view)
    TextView mTvIconMessage;
    @ViewById(R.id.shop_detail_account_balance_icon_text_view)
    TextView mTvIconAccount;
    @ViewById(R.id.shop_detail_order_icon_text_view)
    TextView mTvIconOrder;

    @ViewById(R.id.shop_detail_image_view)
    ImageView mImgShopLogo;
    @ViewById(R.id.shop_detail_name_text_view)
    TextView mTvShopName;

    @ViewById(R.id.shop_detail_order_todo_num_tv)
    TextView mTvOrderTodoNum;

    @ViewById(R.id.shop_detail_order_at_once_num_tv)
    TextView mTvOrderAtOnceNum;

    @ViewById(R.id.shop_detail_order_on_time_num_tv)
    TextView mTvOrderOnTimeNum;

    @ViewById(R.id.shop_detail_product_num_tv)
    TextView mTvProductNum;

    @ViewById(R.id.shop_detail_assistant_num_tv)
    TextView mTvAssistantNum;

    @ViewById(R.id.shop_detail_mailman_num_tv)
    TextView mTvMailmanNum;

    @ViewById(R.id.shop_detail_message_num_tv)
    TextView mTvMessageNum;

    @Extra(ShopKeys.EXTRA_KEY_SHOP_INFO)
    ShopInfo mShopInfo;

    public static void jumpToShopDetail(Context context, ShopInfo shop) {
        Intent intent = new Intent();
        intent.setClass(context, com.qianguo.shop.activity.ShopDetailActivity_.class);
        intent.putExtra(ShopKeys.EXTRA_KEY_SHOP_INFO, shop);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEvent(ShopInfo shop) {
        getShopDetail();
    }

    /**
     * 进入店铺编辑页面
     */
    @Click(R.id.shop_detail_layout)
    void clickShopDetail() {
        AddOrEditShopActivity.jumpToAddOrEditShop(this, mShopInfo);
    }

    /**
     * 进入订单Tab
     */
    @Click(R.id.shop_detail_order_layout)
    void clickOrder() {
        EventBus.getDefault().post(new MainTabEvent(MainTabEvent.TAB_ORDER));
        this.finish();
    }

    /**
     * 进入配送员管理页
     */
    @Click(R.id.shop_detail_mailman_layout)
    void clickMailmen() {
        MailmenManagerActivity.jumpToMailmenManager(this, mShopInfo.getId());
    }

    /**
     * 进入店员管理页
     */
    @Click(R.id.shop_detail_assistant_layout)
    void clickShopAssistant() {
        ShopAssistantManagerActivity.jumpToShopAssistantManager(this, mShopInfo.getId());
    }

    /**
     * 进入商品管理页
     */
    @Click(R.id.shop_detail_product_layout)
    void clickProduct() {
        ProductManagerActivity.jumpToProductManager(this, mShopInfo.getId());
    }

    @Override
    protected String getToolbarTitle() {
        return mShopInfo.getName();
    }

    @Override
    protected void initializeUI() {
        IconfontUtil.setIcon(this, mTvIconProducts, FruitIcon.ICON_PRODUCTS);
        IconfontUtil.setIcon(this, mTvIconAssistants, FruitIcon.ICON_ASSISTANT);
        IconfontUtil.setIcon(this, mTvIconMailMen, FruitIcon.ICON_MAILMAN);
        IconfontUtil.setIcon(this, mTvIconMailMen, FruitIcon.ICON_MAILMAN);
        IconfontUtil.setIcon(this, mTvIconMessage, FruitIcon.ICON_MESSAGE);
        IconfontUtil.setIcon(this, mTvIconAccount, FruitIcon.ICON_BALANCE);

        IconfontUtil.setIcon(this, mTvIconOrder, FruitIcon.ICON_PRODUCTS);

        mImageLoader.loadImage(this, mImgShopLogo, mShopInfo.getLogo());
        mTvShopName.setText(mShopInfo.getName());
        getShopDetail();
    }

    /**
     * 获取店铺详情
     */
    @Background
    void getShopDetail() {
        showLoadingDialog(this);
        mApp.getApi().getShopDetail(mApp.getUser().getToken(), mShopInfo.getId(), new HttpRequestCallback<ShopInfo>() {
            @Override
            public void onSuccess(int code, String msg, ShopInfo data) {
                refreshShopInfo(data);
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(ShopDetailActivity.this, msg);
            }
        });
        closeLoadingDialog();
    }

    @UiThread
    void refreshShopInfo(ShopInfo data) {
        mShopInfo = data;

        mImageLoader.loadImage(this, mImgShopLogo, mShopInfo.getLogo());
        mTvShopName.setText(mShopInfo.getName());
        setToolBarTitle(mShopInfo.getName());

        mTvOrderTodoNum.setText(Integer.toString(data.getOrderStayNum()));
        mTvOrderAtOnceNum.setText(Integer.toString(data.getOrderOnceNum()));
        mTvOrderOnTimeNum.setText(Integer.toString(data.getOrderTimeNum()));
        mTvProductNum.setText(Integer.toString(data.getGoodsNum()));
        mTvAssistantNum.setText(Integer.toString(data.getClerkNum()));
        mTvMailmanNum.setText(Integer.toString(data.getCourierNum()));
        mTvMessageNum.setText(Integer.toString(data.getCmtNum()));
    }

}
